package csu.web.dining.web.servlet;

import csu.web.dining.domain.Cart;
import csu.web.dining.domain.Item;
import csu.web.dining.service.CatalogService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class AddItemToCartServlet extends HttpServlet {

    private static final String CART_FORM = "/WEB-INF/jsp/catalog/cart/cart.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String workingItemId=req.getParameter("workingItemId");
        HttpSession session = req.getSession();
        Cart cart=(Cart) session.getAttribute("cart");
        if(cart==null){
            cart=new Cart();
        }

        if (cart.containsItemId(workingItemId)) {
            //若item在购物车里，就只要更改数量
            cart.incrementQuantityByItemId(workingItemId);
        } else {
            CatalogService catalogService=new CatalogService();
            boolean isInStock = catalogService.isItemInStock(workingItemId);
            Item item = catalogService.getItem(workingItemId);
            cart.addItem(item, isInStock);
        }

        session.setAttribute("cart", cart);
        req.getRequestDispatcher(CART_FORM).forward(req, resp);
    }
}
